/**
 * @brief 串口模块实现
 */

#pragma once

#include <atomic>
#include <string>

#include "designM/command.hpp"
#include "dataCh.hpp"

class serial : public dataCh
{
public:
	enum class parity{ NONE , ODD, EVEN };
	enum class stop{ ONE , ONE_P_FIVE , TWO };
	enum class flow{ NONE , XON_XOFF , RTS_CTS };

private:
	std::string             m_name__;
	std::atomic< int >      m_fd__;
	
	std::atomic< int >      m_baud__;
	std::atomic< int >      m_data_bits__;
	std::atomic< parity >   m_parity__;
	std::atomic< stop >     m_stop__;
	std::atomic< flow >     m_flow__;
private:
	bool update_param( int baud , int dbits , stop s , parity p,  flow f );
public:
	serial( const std::string& name );
	serial( const std::string& name , int baud ,
		int dbits = 8 ,
		stop s = stop::ONE ,
		parity p = parity::NONE,
		flow f = flow::NONE );
	/**
	 * @brief 打开串口
	 */
	bool open();
	bool open( int baud ,
		int dbits = 8 ,
		stop s = stop::ONE ,
		parity p = parity::NONE,
		flow f = flow::NONE );

	bool close();

	size_t send( const char * data , size_t len );

	virtual nativeH_t native_handle() final;

	virtual void onReadyRead( const uint8_t* data , size len ) final;
};
